package cn.wolf.wechat.controller;

import cn.wolf.wechat.util.TokenUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

@RestController
public class indexController {

    RestTemplate restTemplate = new RestTemplate();

    @GetMapping("person")
    public Object person(String code) throws UnsupportedEncodingException {
        String access_token = restTemplate.getForObject(TokenUtil.EXCHANGE_ACCESS_TOKEN
                .replace("APPID", TokenUtil.APP_ID)
                .replace("SECRET", TokenUtil.APP_SECRET)
                .replace("CODE",code), String.class);
        JSONObject jsonObject = JSONObject.parseObject(access_token);
        String user = restTemplate.getForObject(TokenUtil.GET_USER_INFO
                .replace("ACCESS_TOKEN", jsonObject.getString("access_token"))
                .replace("OPENID", jsonObject.getString("openid")), String.class);
        System.out.println(user);
        return "哈哈哈哈";
    }

}
